package servlet;

import dao.ArticleDao;
import dao.CommentDao;
import dao.LabelDao;
import dao.daoimpl.ArticleDaoImpl;
import dao.daoimpl.CommentDaoImpl;
import dao.daoimpl.LabelDaoImpl;
import bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name="DeleteBlogServlet",urlPatterns = "/DeleteBlogServlet")
public class DeleteBlogServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");/*防止乱码*/
        ArticleDao articleDao=new ArticleDaoImpl();/*文章接口实现类*/
        LabelDao labelDao=new LabelDaoImpl();/*标签接口的实现类*/
        CommentDao commentDao=new CommentDaoImpl();/*评论接口的实现类*/
        User user=(User)request.getSession().getAttribute("user");/*获取用户*/
        int articleId=Integer.parseInt(request.getParameter("deleteArticleId"));/*获取文章id*/
        try {
            if(articleDao.deleteArticle(articleId)){
                System.out.println("用户删除博客成功");
                request.getSession().setAttribute("userArticles",articleDao.getArticles(user));
            }else{
                System.out.println("用户删除博客失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
}
